﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ConsolidationTool.Common.Entities;
using System.Collections.Generic;
using ConsolidationTool.Common;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using ConsolidationTool.WebParts.ControlTemplates;

using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Collections;

namespace ConsolidationTool.WebParts.Layouts.CReport.Brochure
{
    public partial class P23INV : FSReportBase
    {

        public override ControlTemplates.CommonEntitySelector TopSelector
        {
            get
            {
                return commonEntitySelector as ControlTemplates.CommonEntitySelector;
            }
        }

        public override Repeater BaseGAP
        {
            get
            {
                return RPGAP;
            }
        }
        public override Repeater BaseReport
        {
            get
            {
                return EQRepeater;
            } 
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void GenerateReport_Click(object sender, EventArgs e)
        {
            base.GenerateReport_Click(sender, e);

            BindLastMonth(TopSelector.lbCoCd.Text, TopSelector.yearTextBox.Text, TopSelector.monthTextBox.Text);
        }


        public override void ExportReport_Click(object sender, EventArgs e)
        {

            P6PL col01 = null;
            col01 = new P6PL();
            col01.row01 = Label01_Day1.Text;
            col01.row02 = Label02_Day1.Text;
            col01.row03 = Label03_Day1.Text;
            col01.row04 = Label04_Day1.Text;
            col01.row05 = Label05_Day1.Text;
            col01.row06 = Label06_Day1.Text;

            P6PL col02 = null;
            col02 = new P6PL();
            col02.row01 = Label01_Day2.Text;
            col02.row02 = Label02_Day2.Text;
            col02.row03 = Label03_Day2.Text;
            col02.row04 = Label04_Day2.Text;
            col02.row05 = Label05_Day2.Text;
            col02.row06 = Label06_Day2.Text;

            P6PL col03 = null;
            col03 = new P6PL();
            col03.row01 = Label01_Day3.Text;
            col03.row02 = Label02_Day3.Text;
            col03.row03 = Label03_Day3.Text;
            col03.row04 = Label04_Day3.Text;
            col03.row05 = Label05_Day3.Text;
            col03.row06 = Label06_Day3.Text;


            P6PL col04 = null;
            col04 = new P6PL();
            col04.row01 = Label01_Day4.Text;
            col04.row02 = Label02_Day4.Text;
            col04.row03 = Label03_Day4.Text;
            col04.row04 = Label04_Day4.Text;
            col04.row05 = Label05_Day4.Text;
            col04.row06 = Label06_Day4.Text;

            P6PL col05 = null;
            col05 = new P6PL();
            col05.row01 = Label01_Day5.Text;
            col05.row02 = Label02_Day5.Text;
            col05.row03 = Label03_Day5.Text;
            col05.row04 = Label04_Day5.Text;
            col05.row05 = Label05_Day5.Text;
            col05.row06 = Label06_Day5.Text;

            P6PL col06 = null;
            col06 = new P6PL();
            col06.row01 = Label01_Day6.Text;
            col06.row02 = Label02_Day6.Text;
            col06.row03 = Label03_Day6.Text;
            col06.row04 = Label04_Day6.Text;
            col06.row05 = Label05_Day6.Text;
            col06.row06 = Label06_Day6.Text;

            var eqr = ViewState["ART_Table_DataSource"] as List<RQDataItem>;
            col05.obj = eqr;

            var RQReportDataSource = ViewState["ART_GAP_DataSource"] as List<RQDataItem>;
            col06.obj = RQReportDataSource;

            string fileName = TopSelector.yearTextBox.Text + TopSelector.monthTextBox.Text + Constants._Brochure_P23_PRIFIX + TopSelector.lbCoCd.Text + ".xlsx";
            BusinessService.CreateP23Excel(LabelTitile00.Text, col01, col02, col03, col04, col05, col06, Label01_Date1.Text, fileName, lbCM.Text, lbYTD.Text);

            string reportUrl = SPContext.Current.Site.Url + "/" + Constants._EXCEL_Library + "/" + fileName;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Message", string.Format("<script>window.open('" + reportUrl + "');</script>"));

        }

        [Serializable]
        public class fsentity
        {
            public bool controlrow { get; set; }
            public bool datarow { get; set; }
            public bool insertcontrol { get; set; }
            public bool bold { get; set; }
            public string unit { get; set; }
            public string accountcode { get; set; }
            public string description { get; set; }
            public string Jan { get; set; }
            public string Feb { get; set; }
            public string Mar { get; set; }
            public string Apr { get; set; }
            public string May { get; set; }
            public string Jun { get; set; }
            public string Jul { get; set; }
            public string Aug { get; set; }
            public string Sep { get; set; }
            public string Oct { get; set; }
            public string Nov { get; set; }
            public string Dec { get; set; }
            public int digits { get; set; }
        }
         
        void BindLastMonth(string coCd, string year, string month)
        {
            string strCurrency = TopSelector.lbCompanyCurrency.Text;
            string strTonnage = TopSelector.lbCompanyTonnage.Text;

            int y_1 = Convert.ToInt32(year) - 1;
            string yearN_1 = Convert.ToString(y_1);

            int y_2 = Convert.ToInt32(year) - 2;
            string yearN_2 = Convert.ToString(y_2);

            string CurrMthDays = Common.BusinessService.GetMthDays(month, year);
            string CurrMthDaysN_1 = Common.BusinessService.GetMthDays(month, yearN_1);
            string CurrMthDaysN_2 = Common.BusinessService.GetMthDays(month, yearN_2);

            LabelTitile00.Text = strCurrency;
            LabelTitile01.Text = strCurrency;

            Label01_Date1.Text = "31/12/" + year;

            Label01_Day1.Text = "0";
            Label01_Day2.Text = "0";
            Label01_Day3.Text = "0";
            Label01_Day4.Text = "0";
            Label01_Day5.Text = "0";
            Label01_Day6.Text = "0";

            Label02_Day1.Text = "0";
            Label02_Day2.Text = "0";
            Label02_Day3.Text = "0";
            Label02_Day4.Text = "0";
            Label02_Day5.Text = "0";
            Label02_Day6.Text = "0";

            Label03_Day1.Text = "0";
            Label03_Day2.Text = "0";
            Label03_Day3.Text = "0";
            Label03_Day4.Text = "0";
            Label03_Day5.Text = "0";
            Label03_Day6.Text = "0";

            Label04_Day1.Text = "0";
            Label04_Day2.Text = "0";
            Label04_Day3.Text = "0";
            Label04_Day4.Text = "0";
            Label04_Day5.Text = "0";
            Label04_Day6.Text = "0";

            Label05_Day1.Text = "0";
            Label05_Day2.Text = "0";
            Label05_Day3.Text = "0";
            Label05_Day4.Text = "0";
            Label05_Day5.Text = "0";
            Label05_Day6.Text = "0";

            Label06_Day1.Text = "0";
            Label06_Day2.Text = "0";
            Label06_Day3.Text = "0";
            Label06_Day4.Text = "0";
            Label06_Day5.Text = "0";
            Label06_Day6.Text = "0";


            SPListItem itemBS = Common.BusinessService.GetItem(coCd, year, month, "Inventory");
            SPListItem itemBSN_1 = Common.BusinessService.GetItem(coCd, yearN_1, month, "Inventory");

            if (itemBSN_1 != null)
            {
                Label01_Day1.Text = itemBSN_1["CM_BS02010403-1"].ToString();
                Label02_Day1.Text = itemBSN_1["CM_BS02010403-2"].ToString();
                Label03_Day1.Text = itemBSN_1["CM_BS02010403-3"].ToString();
                Label04_Day1.Text = itemBSN_1["CM_BS02010403-4"].ToString();
                Label05_Day1.Text = itemBSN_1["CM_BS02010403-5"].ToString();
            }
            if (itemBS != null)
            {
                Label01_Day2.Text = itemBS["CM_BS02010403-1"].ToString();
                Label02_Day2.Text = itemBS["CM_BS02010403-2"].ToString();
                Label03_Day2.Text = itemBS["CM_BS02010403-3"].ToString();
                Label04_Day2.Text = itemBS["CM_BS02010403-4"].ToString();
                Label05_Day2.Text = itemBS["CM_BS02010403-5"].ToString();

                Label01_Day5.Text = itemBS["CM_BS02010406-1"].ToString();
                Label02_Day5.Text = itemBS["CM_BS02010406-2"].ToString();
                Label03_Day5.Text = itemBS["CM_BS02010406-3"].ToString();
                Label04_Day5.Text = itemBS["CM_BS02010406-4"].ToString();
                Label05_Day5.Text = itemBS["CM_BS02010406-5"].ToString();

                Label01_Day6.Text = itemBS["CM_BS02010407-1"].ToString();
                Label02_Day6.Text = itemBS["CM_BS02010407-2"].ToString();
                Label03_Day6.Text = itemBS["CM_BS02010407-3"].ToString();
                Label04_Day6.Text = itemBS["CM_BS02010407-4"].ToString();
                Label05_Day6.Text = itemBS["CM_BS02010407-5"].ToString();
            }

            SPListItem itemBudgetBS = Common.BusinessService.GetBudgetItem(coCd, year, "Budget_Inventory");

            if (itemBudgetBS != null)
            {
                string billData = itemBudgetBS["BillData"].ToString();

                JavaScriptSerializer Serializer = new JavaScriptSerializer();

                List<fsentity> obj = Serializer.Deserialize<List<fsentity>>(billData);

                var objentity = obj as List<fsentity>;

                objentity.ForEach(p =>
                {

                    if (p.accountcode == "BS03010403-1")
                    {
                        Label01_Day3.Text = Convert.ToString(Convert.ToDecimal(p.Dec.ToString()));
                    }
                    if (p.accountcode == "BS03010403-2")
                    {
                        Label02_Day3.Text = Convert.ToString(Convert.ToDecimal(p.Dec.ToString()));
                    }
                    if (p.accountcode == "BS03010403-3")
                    {
                        Label03_Day3.Text = Convert.ToString(Convert.ToDecimal(p.Dec.ToString()));
                    }
                    if (p.accountcode == "BS03010403-4")
                    {
                        Label04_Day3.Text = Convert.ToString(Convert.ToDecimal(p.Dec.ToString()));
                    }
                    if (p.accountcode == "BS03010403-5")
                    {
                        Label05_Day3.Text = Convert.ToString(Convert.ToDecimal(p.Dec.ToString()));
                    }

                });
            }

            int m = Convert.ToInt32(TopSelector.monthTextBox.Text);
            int n = m + 1;

            string Total1 = "0";
            string Total2 = "0";
            string Total3 = "0";
            string Total4 = "0";
            string Total5 = "0";
            for (int i = 1; i <= m; i++)
            {
                string strMonth = i.ToString();

                SPListItem itemInv = Common.BusinessService.GetItem(coCd, year, strMonth, "Inventory");
                if (itemInv != null)
                {
                    Total1 = Convert.ToString(Convert.ToDecimal(Total1) + Convert.ToDecimal(itemInv["CM_BS02010403-1"].ToString()));
                    Total2 = Convert.ToString(Convert.ToDecimal(Total2) + Convert.ToDecimal(itemInv["CM_BS02010403-2"].ToString()));
                    Total3 = Convert.ToString(Convert.ToDecimal(Total3) + Convert.ToDecimal(itemInv["CM_BS02010403-3"].ToString()));
                    Total4 = Convert.ToString(Convert.ToDecimal(Total4) + Convert.ToDecimal(itemInv["CM_BS02010403-4"].ToString()));
                    Total5 = Convert.ToString(Convert.ToDecimal(Total5) + Convert.ToDecimal(itemInv["CM_BS02010403-5"].ToString()));
                }
            }
            for (int i = n; i <= 12; i++)
            {
                string strMonth = i.ToString();

                SPListItem itemInv = Common.BusinessService.GetItem(coCd, yearN_1, strMonth, "Inventory");
                if (itemInv != null)
                {
                    Total1 = Convert.ToString(Convert.ToDecimal(Total1) + Convert.ToDecimal(itemInv["CM_BS02010403-1"].ToString()));
                    Total2 = Convert.ToString(Convert.ToDecimal(Total2) + Convert.ToDecimal(itemInv["CM_BS02010403-2"].ToString()));
                    Total3 = Convert.ToString(Convert.ToDecimal(Total3) + Convert.ToDecimal(itemInv["CM_BS02010403-3"].ToString()));
                    Total4 = Convert.ToString(Convert.ToDecimal(Total4) + Convert.ToDecimal(itemInv["CM_BS02010403-4"].ToString()));
                    Total5 = Convert.ToString(Convert.ToDecimal(Total5) + Convert.ToDecimal(itemInv["CM_BS02010403-5"].ToString()));
                }
            }

            Label01_Day4.Text = (Convert.ToDecimal(Total1) / 12).ToString("n");
            Label02_Day4.Text = (Convert.ToDecimal(Total2) / 12).ToString("n");
            Label03_Day4.Text = (Convert.ToDecimal(Total3) / 12).ToString("n");
            Label04_Day4.Text = (Convert.ToDecimal(Total4) / 12).ToString("n");
            Label05_Day4.Text = (Convert.ToDecimal(Total5) / 12).ToString("n");

            Label06_Day1.Text = Convert.ToString(Convert.ToDecimal(Label01_Day1.Text) + Convert.ToDecimal(Label02_Day1.Text) + Convert.ToDecimal(Label03_Day1.Text) + Convert.ToDecimal(Label04_Day1.Text) + Convert.ToDecimal(Label05_Day1.Text));
            Label06_Day2.Text = Convert.ToString(Convert.ToDecimal(Label01_Day2.Text) + Convert.ToDecimal(Label02_Day2.Text) + Convert.ToDecimal(Label03_Day2.Text) + Convert.ToDecimal(Label04_Day2.Text) + Convert.ToDecimal(Label05_Day2.Text));
            Label06_Day3.Text = Convert.ToString(Convert.ToDecimal(Label01_Day3.Text) + Convert.ToDecimal(Label02_Day3.Text) + Convert.ToDecimal(Label03_Day3.Text) + Convert.ToDecimal(Label04_Day3.Text) + Convert.ToDecimal(Label05_Day3.Text));
            Label06_Day4.Text = Convert.ToString(Convert.ToDecimal(Label01_Day4.Text) + Convert.ToDecimal(Label02_Day4.Text) + Convert.ToDecimal(Label03_Day4.Text) + Convert.ToDecimal(Label04_Day4.Text) + Convert.ToDecimal(Label05_Day4.Text));
            Label06_Day5.Text = Convert.ToString(Convert.ToDecimal(Label01_Day5.Text) + Convert.ToDecimal(Label02_Day5.Text) + Convert.ToDecimal(Label03_Day5.Text) + Convert.ToDecimal(Label04_Day5.Text) + Convert.ToDecimal(Label05_Day5.Text));
            Label06_Day6.Text = Convert.ToString(Convert.ToDecimal(Label01_Day6.Text) + Convert.ToDecimal(Label02_Day6.Text) + Convert.ToDecimal(Label03_Day6.Text) + Convert.ToDecimal(Label04_Day6.Text) + Convert.ToDecimal(Label05_Day6.Text));
        }

    }
}
